<%@ page import="za.org.foobar.famon.model.Application" %>
<%@ page import="za.org.foobar.famon.model.ApplicationGroup" %>
<%@ page import="za.org.foobar.famon.model.ApplicationInstance" %>
<%@ page import="za.org.foobar.famon.model.ApplicationVersion" %>
<%@ page import="za.org.foobar.famon.simpleui.SimpleUiUtil" %>
<%@ page import="za.org.foobar.famon.util.ServerUtil" %>
<%@ page import="za.org.foobar.famon.webapp.ApplicationLogic" %>
<%@ page import="za.org.foobar.famon.webapp.FamonSecurityException" %>
<%
    ApplicationLogic applicationLogic = ApplicationLogic.get();
    String paramApplicationName = ServerUtil.urlDecode(request.getParameter("application_name"));
    String paramInstanceName = ServerUtil.urlDecode(request.getParameter("instance_name"));
    String paramVersionName = ServerUtil.urlDecode(request.getParameter("version_name"));

    if (paramApplicationName == null || paramInstanceName == null || paramVersionName == null) {
        SimpleUiUtil.redirectError(response, "Not all required parameters were provided.");
        return;
    }
    Application app = null;
    try {
        app = applicationLogic.getApplicationByName(paramApplicationName);
        if (app == null) {
            SimpleUiUtil.redirectError(response, "The selected application could not be found");
            return;
        }
    } catch (FamonSecurityException ignored) {
        if (SimpleUiUtil.handleSecurityException(request, response)) {
            return;
        }
    }
    ApplicationInstance applicationInstance = applicationLogic.getApplicationInstanceByName(app, paramInstanceName);
    if (applicationInstance == null) {
        SimpleUiUtil.redirectError(response, "Application Instance not found \"" + paramInstanceName + "\"");
        return;
    }
    ApplicationVersion applicationVersion = applicationLogic.getApplicationVersionByName(app, paramVersionName);
    if (applicationVersion == null) {
        SimpleUiUtil.redirectError(response, "Application Version not found \"" + paramVersionName + "\"");
        return;
    }
    ApplicationGroup applicationGroup = applicationLogic.getApplicationGroup(app.getId(),
            applicationInstance.getId(),
            applicationVersion.getId());
    if (applicationGroup == null) {
        SimpleUiUtil.redirectError(response, "Could not find the specific application group.");
        return;
    }
    response.sendRedirect("index.jsp?applicationGroupId=" + applicationGroup.keyToString());
%>